package com.zhanglifan.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.opensymphony.xwork2.ModelDriven;
import com.zhanglifan.domain.Contract;
import com.zhanglifan.domain.Export;
import com.zhanglifan.domain.ExportProduct;
import com.zhanglifan.service.ContractService;
import com.zhanglifan.service.ExportProductService;
import com.zhanglifan.service.ExportService;
import com.zhanglifan.util.CstUtils;
import com.zhanglifan.util.Page;
import org.apache.struts2.ServletActionContext;

import java.io.IOException;
import java.util.*;

public class ExportAction extends BaseAction implements ModelDriven<Export> {
    private Export export = new Export();
    private ExportService exportService;
    private ExportProductService exportProductService;
    private ContractService contractService;
    private Page page = new Page();

    private String[] mr_id;
    private String[] mr_changed;
    private String[] mr_orderNo;
    private String[] mr_cnumber;
    private String[] mr_grossWeight;
    private String[] mr_netWeight;
    private String[] mr_sizeLength;
    private String[] mr_sizeWidth;
    private String[] mr_sizeHeight;
    private String[] mr_exPrice;
    private String[] mr_tax;

    public String contractList(){
        contractService.findPage("from Contract where state = 1",page,Contract.class,null);
        page.setUrl("exportAction_contractList");
        super.push(page);
        return "contractListSuccess";
    }

    public String tocreate(){
        return "toCreateSuccess";
    }

    public String insert(){
        exportService.saveOrUpdate(export);
        return contractList();
    }

    public String list(){
        exportService.findPage("from Export",page,Export.class,null);
        page.setUrl("exportAction_list");
        super.push(page);
        return "listSuccess";
    }

    public String toview(){
        Export export = exportService.get(Export.class, this.export.getId());
        super.push(export);
        return "toViewSuccess";
    }

    public String toupdate(){
        Export export = exportService.get(Export.class, this.export.getId());
        super.push(export);
        return "toUpdateSuccess";
    }

    public void getExportProducts() throws IOException {
        Export export = exportService.get(Export.class, this.export.getId());
        Set<ExportProduct> exportProducts = export.getExportProducts();

        List<Map<String,Object>> list = new ArrayList<>();
        for (ExportProduct ep : exportProducts) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",ep.getId());
            map.put("productNo",ep.getProductNo());
            map.put("cnumber",ep.getCnumber());
            map.put("grossWeight",ep.getGrossWeight());
            map.put("netWeight",ep.getNetWeight());
            map.put("sizeLength",ep.getSizeLength());
            map.put("sizeWidth",ep.getSizeWidth());
            map.put("sizeHeight",ep.getSizeHeight());
            map.put("exPrice",ep.getExPrice());
            map.put("tax",ep.getTax());
            list.add(map);
        }


        SimplePropertyPreFilter filter = new SimplePropertyPreFilter(ExportProduct.class,"extEproducts","export","factory");
        String jsonStr = JSON.toJSONString(list,filter);
        System.out.println("*** jsonStr 的值是: " + jsonStr + " ***");

        ServletActionContext.getResponse().getWriter().write(jsonStr);

    }

    public String update(){
        for (int i = 0; i < mr_id.length; i++) {
            if("0".equals(mr_changed[i])){
                continue;
            }
            System.out.println("*** mr_id[i] 的值是: " + mr_id[i] + " ***");
            
            ExportProduct exportProduct = exportProductService.get(ExportProduct.class, mr_id[i]);
            exportProduct.setOrderNo(mr_orderNo[i]=="" ? null : Integer.parseInt(mr_orderNo[i]));
            exportProduct.setCnumber(mr_cnumber[i]=="" ? null : Integer.parseInt(mr_cnumber[i]));
            exportProduct.setGrossWeight(mr_grossWeight[i]=="" ? null : Double.parseDouble(mr_grossWeight[i]));
            exportProduct.setNetWeight(mr_netWeight[i]=="" ? null : Double.parseDouble(mr_netWeight[i]));
            exportProduct.setSizeLength(mr_sizeLength[i]=="" ? null : Double.parseDouble(mr_sizeLength[i]));
            exportProduct.setSizeWidth(mr_sizeWidth[i]=="" ? null : Double.parseDouble(mr_sizeWidth[i]));
            exportProduct.setSizeHeight(mr_sizeHeight[i]=="" ? null : Double.parseDouble(mr_sizeHeight[i]));
            exportProduct.setExPrice(mr_exPrice[i]=="" ? null : Double.parseDouble(mr_exPrice[i]));
            exportProduct.setTax(mr_tax[i]=="" ? null : Double.parseDouble(mr_tax[i]));
        }

        Export e = exportService.get(Export.class, this.export.getId());
        e.setInputDate(export.getInputDate());
        e.setLcno(export.getLcno());
        e.setConsignee(export.getConsignee());
        e.setShipmentPort(export.getShipmentPort());
        e.setDestinationPort(export.getDestinationPort());
        e.setTransportMode(export.getTransportMode());
        e.setPriceCondition(export.getPriceCondition());
        e.setMarks(export.getMarks());
        e.setRemark(export.getRemark());
        return list();
    }

    public String delete(){
        Export export = exportService.get(Export.class, this.export.getId());
        String[] ids = export.getContractIds().split(", ");
        CstUtils.changeStatus(contractService,Contract.class,ids,1);
        exportService.deleteById(Export.class, this.export.getId());
        return list();
    }

    public String submit(){
        String[] ids = export.getId().split(", ");
        CstUtils.changeStatus(exportService,Export.class,ids,1);
        return list();
    }

    public String cancel(){
        String[] ids = export.getId().split(", ");
        CstUtils.changeStatus(exportService,Export.class,ids,0);
        return list();
    }

    public String exportE(){
        exportService.exportE(export.getId());
        return list();
    }




    public void setMr_id(String[] mr_id) {
        this.mr_id = mr_id;
    }
    public void setMr_changed(String[] mr_changed) {
        this.mr_changed = mr_changed;
    }

    public void setMr_orderNo(String[] mr_orderNo) {
        this.mr_orderNo = mr_orderNo;
    }

    public void setMr_cnumber(String[] mr_cnumber) {
        this.mr_cnumber = mr_cnumber;
    }

    public void setMr_grossWeight(String[] mr_grossWeight) {
        this.mr_grossWeight = mr_grossWeight;
    }

    public void setMr_netWeight(String[] mr_netWeight) {
        this.mr_netWeight = mr_netWeight;
    }

    public void setMr_sizeLength(String[] mr_sizeLength) {
        this.mr_sizeLength = mr_sizeLength;
    }

    public void setMr_sizeWidth(String[] mr_sizeWidth) {
        this.mr_sizeWidth = mr_sizeWidth;
    }

    public void setMr_sizeHeight(String[] mr_sizeHeight) {
        this.mr_sizeHeight = mr_sizeHeight;
    }

    public void setMr_exPrice(String[] mr_exPrice) {
        this.mr_exPrice = mr_exPrice;
    }

    public void setMr_tax(String[] mr_tax) {
        this.mr_tax = mr_tax;
    }

    public void setExportProductService(ExportProductService exportProductService) {
        this.exportProductService = exportProductService;
    }

    public void setExportService(ExportService exportService) {
        this.exportService = exportService;
    }

    public void setContractService(ContractService contractService) {
        this.contractService = contractService;
    }


    @Override
    public Export getModel() {
        return export;
    }

    public Page<Contract> getPage() {
        return page;
    }
}
